<?php 
include_once('includes.php'); 	


if(isset($_GET["id"]) && $_GET["id"]>0 )
{
	$id = (int) $_GET["id"];
	$sql = "select * from video where id=".addslashes($id);
	$video=db_fetch_one($sql);
	
	if(isset($_GET["task"]) && $_GET["task"] == "delete" )
	{
		db_delete("video",$id );
		Header("Location: /admin/video");
		exit;
	}
	
		
}

if($_SERVER["REQUEST_METHOD"]=="POST")
{
	$id = $_POST["id"];
	$data["id"] = $id;
	$data["title"] = $_POST["title"];
	$data["thumb_image"] = $_POST["thumb_image"];
	
	$data["description"] = $_POST["description"];
	if (get_magic_quotes_gpc()) {
		$data["description"] = stripslashes($_POST["description"]);
	}
	
	$data["youtube_url"] = $_POST["youtube_url"];
	$data["embed_code"] = $_POST["embed_code"];
	if (get_magic_quotes_gpc()) {
		$data["embed_code"] = stripslashes($_POST["embed_code"]);
	}
	
	if($data["id"] >0 )
		db_update("video",$data["id"],$data);
	else
		db_insert("video",$data);		
	
	Header("Location: /admin/video");
	exit;
}

?>
<?php include_once('includes/header.php'); 	 ?>
<script>

function getYoutubeScreen( url, size )
{
	if(url === null){ return ""; }

	size = (size === null) ? "big" : size;
	var vid;
	var results;

	results = url.match("[\\?&]v=([^&#]*)");

	vid = ( results === null ) ? url : results[1];

	if(size == "small"){
	return "http://img.youtube.com/vi/"+vid+"/2.jpg";
	}else {
	return "http://img.youtube.com/vi/"+vid+"/0.jpg";
	}
}

function getYoutubeEmbedCode( url )
{
	if(url === null){ return ""; }

	var vid;
	var results;

	results = url.match("[\\?&]v=([^&#]*)");

	vid = ( results === null ) ? url : results[1];
	return '<iframe width="600" height="400" src="http://www.youtube.com/embed/'+vid+'" frameborder="0" allowfullscreen></iframe>';

}
$(document).ready(function() {
	$('#youtube_url').change(function() {
		var thumb =  getYoutubeScreen($(this).val(), 'small');
		$("#thumb_image").val(thumb);
		
		var embed_code = getYoutubeEmbedCode($(this).val());
		$("#embed_code").val(embed_code);
	});
});

</script>
<span class="title">Create Video</span>


<form name="submitform" method="post" action="">
	<input type="hidden" name="id" value="<?php echo $video->id;?>"/>
	<input type="hidden" name="thumb_image" id="thumb_image" value="<?php echo $video->thumb_image;?>"/>
	
	<p>
		<label>Title</label>
		<input type="text" name="title" value="<?php echo $video->title;?>" style="width:800px"/>
	</p>
	<p>
		<label>Description</label>
		<textarea name="description" style="width:800px; height:100px"><?php echo $video->description;?></textarea>
	</p>
	<fieldset id="youtube">
		<legend>Youtube</legend>
		<p>
			<label>Url</label><br/>
			<input type="text" name="youtube_url" id ="youtube_url"  value="<?php echo $video->youtube_url;?>" style="width:800px"/>
		
		</p>
		<p>
			<label>Embed code</label><br/>
			<textarea name="embed_code" id="embed_code" style="width:800px; height:80px"><?php echo $video->embed_code;?></textarea>
		</p>
		
	</fieldset>		
	
	
	<div class="button"> <input type="submit" value="Save"/>   <input type="button" value="Cancel" onclick="history.go(-1);"/>     </div>
</form>

<?php include_once('includes/footer.php'); 	 ?>